﻿var CommunicationJson;
$(document).ready(function () {
    $.ajax({
        type: "post",
        data: { action: "GetList" },
        datatype: "json",
        url: "../../Ajax/VersionAjax.ashx",
        beforSend: function () {
        },
        success: function (data) {
            if (data != null) {
                var json = $.parseJSON(data).json;
                var tbody = "";
                $.each(json, function (k, v) {
                    tbody += "<tr>"
                    tbody += "<td><lable name='lblName' id='" + v.ID + "'>";
                    tbody += v.Name;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblPid' >";
                    tbody += v.Pid;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblNum' >";
                    tbody += v.Num;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblAddress' >";
                    tbody += v.Address;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsCompel' >";
                    tbody += v.IsCompel;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblIsDisplay' >";
                    tbody += v.IsDisplay;
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblRemark' >";
                    tbody += decodeURIComponent(v.Remark);
                    tbody += "</lable></td>";
                    tbody += "<td><lable name='lblCreateTime'>";
                    tbody += v.CreateTime;
                    tbody += "</lable></td>";
                    tbody += "<td>";
                    tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                    tbody += "</td>";
                    tbody += "</tr>"
                })
                $("#tb1").empty().append(tbody)
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $.ajax({
        type: "post",
        data: { action: "GetListJson" },
        dataType: "json",
        url: "../../Ajax/CommunicationAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            if (data != null) {
                CommunicationJson = data.json;
            }
        },
        complete: function () {
        },
        error: function () {
        }
    });
    $("#btnAdd").click(function () {
        Edit(null);
    })
})
function InitSelectPid(value) {
    var sel = "";
    $.each(CommunicationJson, function (k, v) {
        if ((value == null && k == 0) || v.ID == value)
            sel += "<option value='" + v.ID + "' selected='selected'>" + v.Name + "</option>";
        else
            sel += "<option value='" + v.ID + "'>" + v.Name + "</option>";
    })
    return sel;
}
function Update(contr) {
    var lbl = $(contr).parent().parent().find("lable[name=lblName]").first();
    Edit(lbl);
}
function Edit(lbl) {
    //lbl 是判断是添加还是修改  添加为null
    var sortId = $(lbl).parent().parent().find("lable[name=lblName]").attr("id");
    var PidType = $(lbl).parent().parent().find("lable[name=lblPid]").text();
    var data = new Date();
    var content = "";
    if (lbl != null) {
        content = "</br><table><tr><td>版本名称：<input type='text' id='txtName' value='" + $(lbl).parent().parent().find("lable[name=lblName]").first().text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>版本号：<input type='text' id='txtNum' value='" + $(lbl).parent().parent().find("lable[name=lblNum]").text() + "' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>备注：<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'>" + $(lbl).parent().parent().find("lable[name=lblRemark]").text() + "</textarea></td></tr>"
        content += "<tr><td>版本渠道PID:<select id='selePid'>" + InitSelectPid(PidType) + "</select></td></tr>"
        content += "<tr><td>版本下载地址：<iframe id='contentIframe' height='35px' scrolling='no' frameborder='0' allowtransparency='true' src='../../CommonPage/UpLoadFilePage.aspx' style='margin-top: -10px'></iframe></td></tr></table></br>";
        //        content += "<tr><td>是否强制升级：<input type='text' id='txtIsCompel' value='" + $(lbl).parent().parent().find("lable[name=lblIsCompel]").text() + "' style='width:200px' /></td></tr>"
        //       content += "<tr><td>是否显示任务：<input type='text' id='txtIsDisplay' value='" + $(lbl).parent().parent().find("lable[name=lblIsDisplay]").text() + "' style='width:200px' /></td></tr>"
        //      content += "<tr><td>创建时间：<input type='text' id='txtCreateTime' value='" + $(lbl).parent().parent().find("lable[name=lblCreateTime]").text() + "' style='width:200px' /></td></tr>"
    }
    else {
        content = "<table><tr><td>版本名称：<input type='text' id='txtName' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>版本号:<input type='text' id='txtNum' style='width:200px;float:right' /></td></tr>"
        content += "<tr><td>备注：<textarea  cols='50' rows='10' id='txtRemark' style='width:198px;float:right'></textarea></td></tr>"
        content += "<tr><td>版本渠道PID:<select id='selePid'>" + InitSelectPid() + "</select></td></tr>"
        //      content += "<tr><td>版本下载地址：<input type='text' id='txtAddress' style='width:200px' /></td></tr>"
        content += "<tr><td>版本下载地址:<iframe id='contentIframe' height='35px' scrolling='no'  frameborder='0' allowtransparency='true' src='../../CommonPage/UpLoadFilePage.aspx' style='margin-top: -10px'></iframe></td></tr>"
        content += "<tr><td>是否强制升级：<input type='radio' name='radioIsCompel' id='radioisTure' value='true' /><lable>是</lable> <input type='radio' name='radioIsCompel' id='radioisfalse' value='false'/><lable>否</lable></td></tr>"
        content += "<tr><td>是否显示任务：<input type='radio' name='radioIsDisplay' id='radioisTure' value='true' /><lable>是</lable> <input type='radio' name='radioIsDisplay' id='radioisfalse' value='false'/><lable>否</lable></td></tr>"
        content += "<tr><td>创建时间：<lable name='lblCreateTime'>" + data.getFullYear() + "年" + (data.getMonth() + 1) + "月" + data.getDate() + "日</lable></td></tr></table>"
    }

    $.dialog({
        title: "版本设置模板",
        content: content,
        button: [
                    {
                        name: "提交",
                        callback: function () {
                            AddMould(lbl, $("#txtName"), sortId);
                        }
                    },
                    {
                        name: "取消",
                        callback: function () {
                            this.close();
                        }
                    }
                    ],
        lock: true,
        drag: false
    });
}
function AddMould(contr, sortNum, sortId) {
    var objectId = sortId;
    var objectPid = $(sortNum).parent().parent().parent().find("#selePid[option:selected]").val();
    var objectName = $("#txtName").val();
    var objectNum = $(sortNum).parent().parent().parent().find("#txtNum").val();
    var objectAddress = $(window.frames["contentIframe"].document).find("#txtPath").val();
    var objectIsCompel = $(sortNum).parent().parent().parent().find('input[name=radioIsCompel]:checked').val();
    var objectIsDisplay = $(sortNum).parent().parent().parent().find('input[name=radioIsDisplay]:checked').val();
    var objectRemark = $(sortNum).parent().parent().parent().find("#txtRemark").val();
    var objectCreateTime = $(sortNum).parent().parent().parent().find("lable[name=lblCreateTime]").text();
    var oldImgPath = $(contr).parent().parent().find("lable[name=lblAddress]").text();
    var data = contr == null ? { action: "Add", name: objectName, Address: objectAddress, Num: objectNum, IsCompel: objectIsCompel, IsDisplay: objectIsDisplay, Remark: objectRemark, CreateTime: objectCreateTime, Pid: objectPid} :
              { action: "Update", id: objectId, name: objectName, Address: objectAddress, Num: objectNum, Remark: objectRemark, oldImgPath: oldImgPath, Pid: objectPid };
    $.ajax({
        type: "post",
        data: data,
        async: true,
        url: "../../Ajax/VersionAjax.ashx",
        beforeSend: function () {
        },
        success: function (data) {
            $.dialog.tips("更新成功！", 1);
            if (contr == null) {
                var info = data.split('|'); //id|imgPath
                var tbody = "<tr>";
                tbody += "<td><lable name='lblName' id='" + info[0] + "'>";
                tbody += objectName;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblPid'>";
                tbody += objectPid;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblNum'>";
                tbody += objectNum;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblAddress'>";
                tbody += info[1];
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblIsCompel'>";
                tbody += objectIsCompel;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblIsDisplay'>";
                tbody += objectIsDisplay;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblRemark'>";
                tbody += objectRemark;
                tbody += "</lable></td>";
                tbody += "<td><lable name='lblCreateTime'>";
                tbody += objectCreateTime;
                tbody += "</lable></td>";
                tbody += "<td>";
                tbody += "<input type='button' onclick='Update(this)' value='更新'><input type='button' onclick='Delete(this)' value='删除'>"
                tbody += "</td>";
                tbody += "</tr>"
                $("#tb1").append(tbody);
            }
            else {
                $(contr).parent().parent().find("lable[name=lblName]").first().text(objectName);
                $(contr).parent().parent().find("lable[name=lblPid]").first().text(objectPid);
                $(contr).parent().parent().find("lable[name=lblNum]").first().text(objectNum);
                $(contr).parent().parent().find("lable[name=lblAddress]").first().text(oldImgPath);
                $(contr).parent().parent().find("lable[name=lblIsCompel]").first().text(objectIsCompel);
                $(contr).parent().parent().find("lable[name=lblIsDisplay]").first().text(objectIsDisplay);
                $(contr).parent().parent().find("lable[name=lblRemark]").first().text(objectRemark);
                $(contr).parent().parent().find("lable[name=lblCreateTime]").first().text(objectCreateTime);
            }
        },
        complete: function () {
        },
        error: function () {
        }
    })
}
function Delete(contr) {
    var oldImgPath = $(contr).parent().parent().find("lable[name=lblAddress]").text();
    var id = $(contr).parent().parent().find("lable[name=lblName]").first().attr("id");
    if (window.confirm("确认删除？")) {
        $.ajax({
            type: "post",
            data: { action: "Delete", id: id, imgPath: oldImgPath },
            async: true,
            url: "../../Ajax/VersionAjax.ashx",
            beforeSend: function () {
            },
            success: function (data) {
                if (data == "True") {
                    $(contr).parent().parent().remove();
                    $.dialog.tips("删除成功！", 1);
                }
                else {
                    $.dialog.tips("删除失败！", 1);
                }
            },
            complete: function () {
            },
            error: function () {
            }
        })
    }
}

